<?php

/***
** 学生model
*/
class Student_model extends CI_Model{

 	function __construct(){
    	parent::__construct();
	}

  /**
  ** 获取学生信息
  */
  function get_student_by_userid($user_id){
    $sql = "SELECT * FROM {$this->db->dbprefix}o2o_student where user_id = {$user_id} limit 1";
    return $this->db->query($sql)->row_array();
  }

  /**
   * 根据校区获取学员list
   * @param  [type] $campus_id [description]
   * @return [type]           [description]
   */
  public function get_student_simple_by_campus($campus_id)
  {
    $sql = "SELECT student_id,name FROM {$this->db->dbprefix}o2o_school_student where school_campus_id = {$campus_id}";
    return $this->db->query($sql)->result_array();
  }

  /**
   * 获取学生详情
   * @param  [type] $student_id [description]
   * @return [type]             [description]
   */
  public function get_student_by_id($student_id)
  {
    $sql = "SELECT  c.name as campus_name,stu.student_id,stu.name,stu.user_id,stu.phone,stu.sex,stu.birthday,stu.wx,stu.qq,stu.edu,stu.on_job,stu.class_time,stu.remark,stu.address,sstu.sort,sstu.status,sstu.add_time
     FROM {$this->db->dbprefix}o2o_student  stu
     left join {$this->db->dbprefix}o2o_school_student sstu on sstu.student_id = stu.student_id
     left join {$this->db->dbprefix}o2o_school_campus c on c.id = sstu.school_campus_id
     where stu.student_id = {$student_id} limit 1";
    return $this->db->query($sql)->row_array();
  }

  /**
   * 添加线下学员
   * @param [type] $data      [description]
   * @param [type] $school_id [description]
   */
  public function add_student_offline($data,$school_id)
  {

    if(empty($data['phone'])) return -1;//手机号不能为空
    //事务开始
    $this->db->trans_start();
    $user_id = 0;
    $student_id = 0;
    //1、判读学员账号是否存在
    $user = $this->db->query("SELECT * FROM {$this->db->dbprefix}user where mobile = {$data['phone']} limit 1")->row_array();
    if(empty($user)){
        //user信息为空,插入新user
        $user_data = array(
        'login_name' => '',
        'rand_str' => '',
        'password' => '',
        'mobile' => $data['phone'],
        'is_reg_xmpp' => 0,
        'sex' => $data['sex'],
        'register_ip' => '',
        'status' => 1,
        'add_time' => time(),
        'edit_time' => 0,
      );
      $this->db->insert('user', $user_data);
      $user_id = $this->db->insert_id();

      //user_account
      $user_account = array(
        'user_id' => $user_id,
        'balance' => 0,
        'integral' => 0,
        'commission_total' => 0
      );
      $this->db->insert('user_account', $user_account);
    }else{
      $user_id = $user['id'];
    }

    //2判断学生信息是否存在
    $sql = "SELECT * FROM {$this->db->dbprefix}o2o_student where user_id = {$user_id} limit 1";
    $student_info = $this->db->query($sql)->row_array();
    if(empty($student_info)){
      //不存在，添加
      $student = array(
        'user_id' => $user_id,
        'name' => $data['name'],
        'phone' => $data['phone'],
        'birthday' => $data['birthday'],
        'sex' => $data['sex'],
        'wx' => $data['wx'],
        'qq' => $data['qq'],
        'edu' => $data['edu'],
        'on_job' => $data['on_job'],
        'workstation' => '',
        'class_time' => json_encode( $data['class_time']),
        'address' => $data['address'],
        'remark' => $data['remark'],
        'add_time' => time()
      );

      $this->db->insert('o2o_student', $student);
      $student_id = $this->db->insert_id();
    }else{
      $student_id = $student_info['student_id'];
      //存在，update
      $student = array(
        'name' => $data['name'],
        'phone' => $data['phone'],
        'birthday' => $data['birthday'],
        'sex' => $data['sex'],
        'wx' => $data['wx'],
        'qq' => $data['qq'],
        'edu' => $data['edu'],
        'on_job' => $data['on_job'],
        'address' => $data['address'],
        'class_time' => json_encode($data['class_time[]']),
        'remark' => $data['remark'],
      );

      $this->db->update('o2o_student', $student,array('student_id' => $student_info['student_id']));
    }
    //3、将学员添加到school
    $res = $this->db->query("SELECT COUNT(0) as count FROM {$this->db->dbprefix}o2o_school_student WHERE school_id = {$school_id} AND student_id={$student_id}")->row_array();
    $school_stu = $res['count'];
    if($school_stu == 0){
      //不存在，添加
      $s_stu = array(
        'school_id' => $school_id,
        'school_campus_id' => $data['campus'],
        'user_id' => $user_id,
        'student_id' => $student_id,
        'from' => 1,
        'name' => $data['name'],
        'mobile' => $data['phone'],
        'address' => $data['address'],
        'sort' => 0,
        'status' => 1,
        'add_time' => time(),
      );
      $this->db->insert('o2o_school_student', $s_stu);
    }


    $this->db->trans_complete();
    return $student_id;
  }

  /**
   * 学校手动添加线下学员
   * @param [type] $data    [description]
   */
  public function add_student_user_to_class($data,$class_id,$school_id)
  {
    if(empty($data['phone'])) return -1;//手机号不能为空
    //事务开始
    $this->db->trans_start();
    $user_id = 0;
    $student_id = 0;
    //1、判读学员账号是否存在
    $user = $this->db->query("SELECT * FROM {$this->db->dbprefix}user where mobile = {$data['phone']} limit 1")->row_array();
    if(empty($user)){
        //user信息为空,插入新user
        $user_data = array(
        'login_name' => '',
        'rand_str' => '',
        'password' => '',
        'mobile' => $data['phone'],
        'is_reg_xmpp' => 0,
        'sex' => $data['sex'],
        'register_ip' => '',
        'status' => 1,
        'add_time' => time(),
        'edit_time' => 0,
      );
      $this->db->insert('user', $user_data);
      $user_id = $this->db->insert_id();

      //user_account
      $user_account = array(
        'user_id' => $user_id,
        'balance' => 0,
        'integral' => 0,
        'commission_total' => 0
      );
      $this->db->insert('user_account', $user_account);
    }else{
      $user_id = $user['id'];
    }

    //2判断学生信息是否存在
    $sql = "SELECT * FROM {$this->db->dbprefix}o2o_student where user_id = {$user_id} limit 1";
    $student_info = $this->db->query($sql)->row_array();
    if(empty($student_info)){
      //不存在，添加
      $student = array(
        'user_id' => $user_id,
        'name' => $data['name'],
        'phone' => $data['phone'],
        'birthday' => $data['birthday'],
        'sex' => $data['sex'],
        'wx' => $data['wx'],
        'qq' => $data['qq'],
        'edu' => $data['edu'],
        'on_job' => $data['on_job'],
        'workstation' => '',
        'class_time' => json_encode( $data['class_time']),
        'address' => $data['address'],
        'remark' => $data['remark'],
        'add_time' => time()
      );

      $this->db->insert('o2o_student', $student);
      $student_id = $this->db->insert_id();
    }else{
      $student_id = $student_info['student_id'];
      //存在，update
      $student = array(
        'name' => $data['name'],
        'phone' => $data['phone'],
        'birthday' => $data['birthday'],
        'sex' => $data['sex'],
        'wx' => $data['wx'],
        'qq' => $data['qq'],
        'edu' => $data['edu'],
        'on_job' => $data['on_job'],
        'address' => $data['address'],
        'class_time' => json_encode( $data['class_time']),
        'remark' => $data['remark'],
      );

      $this->db->update('o2o_student', $student,array('student_id' => $student_info['student_id']));
    }
    //3、将学员添加到school
    $res = $this->db->query("SELECT COUNT(0) as count FROM {$this->db->dbprefix}o2o_school_student WHERE school_id = {$school_id} AND student_id={$student_id}")->row_array();
    $school_stu = $res['count'];
    if($school_stu == 0){
      //不存在，添加
      $s_stu = array(
        'school_id' => $school_id,
        'user_id' => $user_id,
        'student_id' => $student_id,
        'from' => 1,
        'name' => $data['name'],
        'mobile' => $data['phone'],
        'address' => $data['address'],
        'sort' => 0,
        'status' => 1,
        'add_time' => time(),
      );
      $this->db->insert('o2o_school_student', $s_stu);
    }
    //4、将学员添加到class
    $res = $this->db->query("SELECT COUNT(0) as count FROM {$this->db->dbprefix}o2o_class_student WHERE class_id = {$class_id} AND student_id={$student_id}")->row_array();
    $class_stu = $res['count'];
    if($school_stu == 0){
      //不存在，添加
      $c_stu = array(
        'class_id' => $class_id,
        'student_id' => $student_id,
        'from' => 1,
        'class_title' => '',
        'sort' => 0,
        'status' => 1,
        'add_time' => time(),
      );
      $this->db->insert('o2o_class_student', $c_stu);
    }

    $this->db->trans_complete();
    return 1;
  }
}
